//
//  TomorrowViewController.m
//  quanminzhekou
//
//  Created by zxl on 15/1/4.
//  Copyright (c) 2015年 zhongqinglongtu. All rights reserved.
//

#import "TomorrowViewController.h"
#import "CommonTableViewCell.h"
#import "CommonEngine.h"
#import "CommonCellViewModel.h"
#import "CommonHeaderAndFooterView.h"

@interface TomorrowViewController ()
@property (nonatomic) NSMutableArray* tomorrowDataArr;
@property (nonatomic) MKNetworkOperation* tomorrowOperation;
@end

@implementation TomorrowViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self.tableView registerClass:[CommonHeaderAndFooterView class] forHeaderFooterViewReuseIdentifier:@"CommonHeaderView"];
    _tomorrowDataArr = [[NSMutableArray alloc] init];
    
    self.engineModel.zqStr = @"5";
    [self updateData];
}

- (void)viewDidLayoutSubviews {
    [super viewDidLayoutSubviews];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
}

- (void)dealloc
{
    [self.tomorrowOperation cancel];
}

#pragma mark - super
- (void)updateData {
    [super updateData];
    
    if (_tomorrowOperation.isExecuting) {
        return;
    }
    
    __weak typeof(self) weakSelf = self;
    
    NSString* path = [[CommonEngine sharedInstance] urlPathForDataWithDictionary:self.engineModel.dic];
    
    _tomorrowOperation = [[CommonEngine sharedInstance] getOperationWithURLPath:path shouldCache:NO completionHandler:^(NSDictionary *result) {
        NSNumber* status = [result objectForKey:@"status"];
        NSNumber* count = [result objectForKey:@"count"];
        if (1 == status.integerValue) {
            
            NSMutableArray* res = [NSMutableArray new];
            
            NSArray* arr = [result objectForKey:@"res"];
            for (NSDictionary* dic in arr) {
                CommonCellModel* model = [[CommonCellModel alloc] initWithDic:dic];
                CommonCellViewModel* viewModel = [[CommonCellViewModel alloc] initWithCommonCellModel:model];
                [res addObject:viewModel];
            }
            
            if (weakSelf.engineModel.isNeedReloadData) {
                weakSelf.tomorrowDataArr = res;
                weakSelf.engineModel.isNeedReloadData = NO;
            } else {
                [weakSelf.tomorrowDataArr addObjectsFromArray:res];
            }
            
            dispatch_async(dispatch_get_main_queue(), ^{
                [weakSelf.tableView reloadData];
                [weakSelf.tableView resetReloadMoreViewFrame];
                if (TableViewUpdateTypeRefresh == weakSelf.tableView.type) {
                    [weakSelf finishRefreshData];
                } else if (TableViewUpdateTypeReloadMore == weakSelf.tableView.type) {
                    [weakSelf finishReloadMoreDataWithIsEnd:(0 == count.integerValue)];
                }
                
                weakSelf.viewControllerType = ViewControllerNormalType;
            });
        }
    } errorHandler:^(NSError *errMsg) {
        NSLog(@"%@", errMsg);
        
        dispatch_async(dispatch_get_main_queue(), ^{
            [weakSelf.tableView resetReloadMoreViewFrame];
            if (TableViewUpdateTypeRefresh == weakSelf.tableView.type) {
                [weakSelf finishRefreshData];
            } else if (TableViewUpdateTypeReloadMore == weakSelf.tableView.type) {
                [weakSelf finishReloadMoreDataWithIsEnd:NO];
            }
            
            weakSelf.viewControllerType = ViewControllerNoNetWorkType;
        });
    }];
}

#pragma mark -

#pragma mark - UITableViewDataSource
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.tomorrowDataArr.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    CommonTableViewCell *cell = (CommonTableViewCell*)[tableView dequeueReusableCellWithIdentifier:@"TomorrowCell"];
    cell.isHasSelectedType = NO;
    
    CommonCellViewModel* viewModel = [self.tomorrowDataArr objectAtIndex:indexPath.row];
    [cell layoutSubviewDataWithViewModel:viewModel];
    
    return cell;
}

#pragma mark - UITableViewDelegate
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
    CommonHeaderAndFooterView* headerView = [tableView dequeueReusableHeaderFooterViewWithIdentifier:@"CommonHeaderView"];
    
    NSDate* date = [NSDate date];
    NSDateFormatter* dateFormatter = [[NSDateFormatter alloc] init];
    [dateFormatter setDateFormat:@"HH:mm"];
    NSInteger hour = [dateFormatter stringFromDate:date].integerValue;
    
    if (9 <= hour) {
        date = [NSDate dateWithTimeIntervalSinceNow:24 * 3600];
    }
    
    [dateFormatter setDateFormat:@"yyyy"];
    NSString* year = [dateFormatter stringFromDate:date];
    [dateFormatter setDateFormat:@"MM"];
    NSString* month = [dateFormatter stringFromDate:date];
    [dateFormatter setDateFormat:@"dd"];
    NSString* day = [dateFormatter stringFromDate:date];
    
    headerView.titleLab.text = [NSString stringWithFormat:@"--- %@年%@月%@日 09:00 准时开售---", year, month, day];
    return headerView;
}

@end
